package com.springcloud.controller;

import com.hongdou.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


import java.util.List;

@RestController
@RequestMapping("consumer")
public class DeptController {

    //private static final String REST_URL_PREFIX = "http://localhost:8081";
    //使用了ribbon,这里的地址要更改为Eureka中application的名字
    private static final String REST_URL_PREFIX = "http://CLOUD-PROVIDER";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/dept/add")
    public  boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add/", dept, Boolean.class);
    }

    @GetMapping(value = "/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id,Dept.class);
    }

    @GetMapping("/dept/list")
    public List<Dept> list(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

    @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
    public Object getDiscovery(){
     return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
    }

}
